package com.ts.api.http.weixin;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;

@Component
public class WeiXin {

    public String getOpenid(String appid, String secret,String code) {
        final String api_url = "https://api.weixin.qq.com/sns/jscode2session";
        String url = String.format("%s?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", api_url, appid, secret, code);
        HttpResponse build = HttpClientBuilder.builder(url)
                .withLogLevel(LogLevel.WARN)
                .withErrorLogLevel(LogLevel.WARN)
                .build();
        String str = build.getStr();
        JSONObject jsonObject = JSONObject.parseObject(str);
        String openid = jsonObject.getString("openid");

        return openid;
    }
}
